package Chat;

import java.util.ArrayList;
import javax.swing.*;
import javax.swing.UIManager.LookAndFeelInfo;

/**
 *
 * @author danielsl & thorerikmidtb
 */
class GUI extends JFrame {

    private JList userList;
    private JScrollPane inputTextPane;
    private JScrollPane userScrollPane;
    private JScrollPane mainChatPane;
    private JTextArea textAreaMainChat;
    private JTextField inputText;
    private DefaultListModel listModel;
    private JMenuItem connectMenuItem;
    private JRadioButtonMenuItem defaultTheme;
    private JMenu editMenu;
    private JRadioButtonMenuItem girlyTheme;
    private JMenuBar jMenuBar;
    private JRadioButtonMenuItem matrixTheme;
    private JMenu menuMenu;
    private JScrollPane pmChatPane;
    private JMenuItem quitMenuItem;
    private JTabbedPane tabbedMainChat;
    private JMenu themesMenu;
    private ButtonGroup themesGroup;
    private PMchatWindow newPM;
    private ArrayList<PMchatWindow> pmTabList;

    public GUI() {
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            System.out.println("NIMBUS ERRORED OUT!");
        }
        drawGUI();
    }

    public void drawGUI() {
        userList = new JList();
        userScrollPane = new JScrollPane();
        mainChatPane = new JScrollPane();
        textAreaMainChat = new JTextArea();
        inputText = new JTextField();
        listModel = new DefaultListModel();
        jMenuBar = new JMenuBar();
        defaultTheme = new JRadioButtonMenuItem();
        connectMenuItem = new JMenuItem();
        editMenu = new JMenu();
        matrixTheme = new JRadioButtonMenuItem();
        menuMenu = new JMenu();
        girlyTheme = new JRadioButtonMenuItem();
        pmChatPane = new JScrollPane();
        quitMenuItem = new JMenuItem();
        tabbedMainChat = new JTabbedPane();
        themesMenu = new JMenu();
        themesGroup = new ButtonGroup();
        pmTabList = new ArrayList<PMchatWindow>();

        // Sets the frame configuration
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("distributed Objective Realtime Chat (dORC)");
        setSize(new java.awt.Dimension(1024, 720));
        setResizable(true);
        setLocationRelativeTo(null);

        userScrollPane.setViewportView(userList);

        inputText.setAutoscrolls(false);
        inputText.setColumns(1);
        inputText.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));


        userList = new JList(listModel);
        userList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        userList.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));



        /* This will make the textAreaMainChat print out the words on the next line, if the sentence is long plus div.*/
        textAreaMainChat.setColumns(20);
        textAreaMainChat.setEditable(false);
        textAreaMainChat.setRows(5);
        textAreaMainChat.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        textAreaMainChat.setWrapStyleWord(true);
        textAreaMainChat.setLineWrap(true);
        textAreaMainChat.setFont(new java.awt.Font("Times New Roman", 0, 13));
        textAreaMainChat.setCaretPosition(textAreaMainChat.getDocument().getLength());


        tabbedMainChat.addTab("Main Chat", mainChatPane);
        tabbedMainChat.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);


        mainChatPane.setViewportView(textAreaMainChat);

        /*This creates the menu toolbar at the top with the menus */
        menuMenu.setBorder(null);
        menuMenu.setText("Menu");
        menuMenu.add(connectMenuItem);
        menuMenu.add(quitMenuItem);

        connectMenuItem.setText("Connect");


        quitMenuItem.setText("Quit");

        jMenuBar.add(menuMenu);


        editMenu.setBorder(null);
        editMenu.setText("Edit");

        themesMenu.setText("Themes");

        /* Groups the Radio buttons */
        themesGroup.add(defaultTheme);
        themesGroup.add(matrixTheme);
        themesGroup.add(girlyTheme);

        defaultTheme.setSelected(true);
        defaultTheme.setText("Default");
        themesMenu.add(defaultTheme);


        matrixTheme.setText("Matrix");
        matrixTheme.setToolTipText("Changes theme to Matrix-theme.");

        themesMenu.add(matrixTheme);

        girlyTheme.setText("Girly");
        themesMenu.add(girlyTheme);

        editMenu.add(themesMenu);

        jMenuBar.add(editMenu);

        setJMenuBar(jMenuBar);

        mainChatPane.setViewportView(textAreaMainChat);
        userScrollPane.setViewportView(userList);




        /* The layout */

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(inputText, javax.swing.GroupLayout.DEFAULT_SIZE, 522, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tabbedMainChat, javax.swing.GroupLayout.DEFAULT_SIZE, 469, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(userScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tabbedMainChat, javax.swing.GroupLayout.DEFAULT_SIZE, 358, Short.MAX_VALUE)
                    .addComponent(userScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 50, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(inputText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        setVisible(true);
    }

    /**
     * This method will create a new chat tab that includes a JTextArea and JScrollPane.
     * This is where the private conversations takes place.
     * @param userName
     * @return
     */
    public PMchatWindow createNewChatTab(String userName) {
        newPM = new PMchatWindow(tabbedMainChat, userName);
        pmTabList.add(newPM);
        return newPM;

    }


    /**
     * This method will set the theme to Matrix. It will also check if there are
     * any tabs created. If so, then it will also change the appearance of those
     * text areas.
     */
    public void setThemeMatrix() {
        textAreaMainChat.setBackground(new java.awt.Color(0, 0, 0));
        textAreaMainChat.setForeground(new java.awt.Color(0, 255, 0));

        inputText.setBackground(new java.awt.Color(0, 0, 0));
        inputText.setForeground(new java.awt.Color(0, 255, 0));

        userList.setBackground(new java.awt.Color(0, 0, 0));
        userList.setForeground(new java.awt.Color(0, 255, 0));

        textAreaMainChat.setFont(new java.awt.Font("Rod", 0, 13));

        if (!pmTabList.isEmpty()) {
            for (int i = 0; i < pmTabList.size(); i++) {
            pmTabList.get(i).getPmTextArea().setBackground(new java.awt.Color(0, 0, 0));
            pmTabList.get(i).getPmTextArea().setForeground(new java.awt.Color(0, 255, 0));
            pmTabList.get(i).getPmTextArea().setFont(new java.awt.Font("Rod", 0, 13));
            }
        }


    }
    /**
     * This method will set the theme to Default. It will also check if there are
     * any tabs created. If so, then it will also change the appearance of those
     * text areas.
     */

    public void setThemeDefault() {
        textAreaMainChat.setBackground(new java.awt.Color(255, 255, 255));
        textAreaMainChat.setForeground(new java.awt.Color(0, 0, 0));

        inputText.setBackground(new java.awt.Color(255, 255, 255));
        inputText.setForeground(new java.awt.Color(0, 0, 0));

        userList.setBackground(new java.awt.Color(255, 255, 255));
        userList.setForeground(new java.awt.Color(0, 0, 0));

        textAreaMainChat.setFont(new java.awt.Font("Times New Roman", 0, 13));

        if (!pmTabList.isEmpty()) {
            for (int i = 0; i < pmTabList.size(); i++) {
            pmTabList.get(i).getPmTextArea().setBackground(new java.awt.Color(255, 255, 255));
            pmTabList.get(i).getPmTextArea().setForeground(new java.awt.Color(0, 0, 0));
            pmTabList.get(i).getPmTextArea().setFont(new java.awt.Font("Times New Roman", 0, 13));
            }
        }


    }

    /**
     * This method will set the theme to Girly. It will also check if there are
     * any tabs created. If so, then it will also change the appearance of those
     * text areas.
     */
    public void setThemeGirly() {
        textAreaMainChat.setBackground(new java.awt.Color(255, 171, 251));
        textAreaMainChat.setForeground(new java.awt.Color(181, 0, 163));

        inputText.setBackground(new java.awt.Color(255, 171, 251));
        inputText.setForeground(new java.awt.Color(181, 0, 163));

        userList.setBackground(new java.awt.Color(255, 171, 251));
        userList.setForeground(new java.awt.Color(181, 0, 163));

        textAreaMainChat.setFont(new java.awt.Font("Monotype Corsiva", 0, 13)); // NOI18N

        if (!pmTabList.isEmpty()) {
            for (int i = 0; i < pmTabList.size(); i++) {
            pmTabList.get(i).getPmTextArea().setBackground(new java.awt.Color(255, 171, 251));
            pmTabList.get(i).getPmTextArea().setForeground(new java.awt.Color(181, 0, 163));
            pmTabList.get(i).getPmTextArea().setFont(new java.awt.Font("Monotype Corsiva", 0, 13));
            }
        }

    }
    public JMenuItem getConnectMenuItem() {
        return connectMenuItem;
    }

    public JRadioButtonMenuItem getDefaultTheme() {
        return defaultTheme;
    }

    public JMenu getEditMenu() {
        return editMenu;
    }

    public JRadioButtonMenuItem getGirlyTheme() {
        return girlyTheme;
    }

    public JTextField getInputText() {
        return inputText;
    }

    public JMenuBar getjMenuBar() {
        return jMenuBar;
    }

    public JScrollPane getinputTextPane() {
        return inputTextPane;
    }

    public DefaultListModel getListModel() {
        return listModel;
    }

    public JScrollPane getMainChatPane() {
        return mainChatPane;
    }

    public JRadioButtonMenuItem getMatrixTheme() {
        return matrixTheme;
    }

    public JMenu getMenuMenu() {
        return menuMenu;
    }

    public JScrollPane getPmChatPane() {
        return pmChatPane;
    }

    public JMenuItem getQuitMenuItem() {
        return quitMenuItem;
    }

    public JTabbedPane getTabbedMainChat() {
        return tabbedMainChat;
    }

    public JTextArea getTextAreaMainChat() {
        return textAreaMainChat;
    }

    public JMenu getThemesMenu() {
        return themesMenu;
    }

    public JList getUserList() {
        return userList;
    }

    public JScrollPane getUserScrollPane() {
        return userScrollPane;
    }

    public void setConnectMenuItem(JMenuItem connectMenuItem) {
        this.connectMenuItem = connectMenuItem;
    }

    public void setDefaultTheme(JRadioButtonMenuItem defaultTheme) {
        this.defaultTheme = defaultTheme;
    }

    public void setEditMenu(JMenu editMenu) {
        this.editMenu = editMenu;
    }

    public void setGirlyTheme(JRadioButtonMenuItem girlyTheme) {
        this.girlyTheme = girlyTheme;
    }

    public void setInputText(JTextField inputText) {
        this.inputText = inputText;
    }

    public void setjMenuBar(JMenuBar jMenuBar) {
        this.jMenuBar = jMenuBar;
    }

    public void setinputTextPane(JScrollPane inputTextPane) {
        this.inputTextPane = inputTextPane;
    }

    public void setListModel(DefaultListModel listModel) {
        this.listModel = listModel;
    }

    public void setMainChatPane(JScrollPane mainChatPane) {
        this.mainChatPane = mainChatPane;
    }

    public void setMatrixTheme(JRadioButtonMenuItem matrixTheme) {
        this.matrixTheme = matrixTheme;
    }

    public void setMenuMenu(JMenu menuMenu) {
        this.menuMenu = menuMenu;
    }

    public void setPmChatPane(JScrollPane pmChatPane) {
        this.pmChatPane = pmChatPane;
    }

    public void setQuitMenuItem(JMenuItem quitMenuItem) {
        this.quitMenuItem = quitMenuItem;
    }

    public void setTabbedMainChat(JTabbedPane tabbedMainChat) {
        this.tabbedMainChat = tabbedMainChat;
    }

    public void setTextAreaMainChat(JTextArea textAreaMainChat) {
        this.textAreaMainChat = textAreaMainChat;
    }

    public void setThemesMenu(JMenu themesMenu) {
        this.themesMenu = themesMenu;
    }

    public void setUserList(JList userList) {
        this.userList = userList;
    }

    public void setUserScrollPane(JScrollPane userScrollPane) {
        this.userScrollPane = userScrollPane;
    }

    /**
     *  This class is a tab that contains a JTextArea and a JScrollPane.
     */
    public class PMchatWindow {

        private JTextArea area;
        private String userName;
        private JScrollPane pane;

        public PMchatWindow() {
        }

        /**
         * This constructor creates the tab and adds it to the wanted
         * JTabbedPane.
         * @param tabbedPane
         * @param userName
         */

        public PMchatWindow(JTabbedPane tabbedPane, String userName) {
            area = new JTextArea();
            pane = new JScrollPane();
            this.userName = userName;

            area.setColumns(20);
            area.setEditable(false);
            area.setRows(5);
            area.setText("You are now talking to " + userName + ".\n");
            area.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
            pane.setViewportView(area);

            area.setFont(textAreaMainChat.getFont());
            area.setBackground(textAreaMainChat.getBackground());
            area.setForeground(textAreaMainChat.getForeground());
            area.setWrapStyleWord(true);
            area.setLineWrap(true);

            tabbedPane.add(userName, pane);
            pane.setVisible(true);
            area.setVisible(true);

        }

        public String getPmUserName() {
            return userName;
        }

        public JTextArea getPmTextArea() {
            return area;
        }

        public JScrollPane getPmPane() {
            return pane;
        }

        public void setPmUserName(String newName) {
            this.userName = newName;
        }
    }
}
